﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dot5Demo
{
    public static class UICultureExtensions
    {
        public static void UserCulture(this object obj)
        {
            if (obj == null)
                return;
            if (obj is Control)
            {
                (obj as Control).UseCulture();
            }
            else if (obj is Component)
            {
                (obj as Component).UseCulture();
            }
        }

        public static void UseCulture(this Control ctr)
        {
            if (ctr == null)
                return;
            ctr.Text = ctr.Text.CultureString();
            if (ctr.Controls.Count > 0)
            {
                foreach (Control c in ctr.Controls)
                {
                    c.UseCulture();
                }
            }
            if (ctr is ToolStrip)
            {
                var ts = ctr as ToolStrip;
                if (ts.Items != null && ts.Items.Count > 0)
                {
                    foreach (ToolStripItem item in ts.Items)
                    {
                        item.UseCulture();
                    }
                }
            }
        }

        public static void UseCulture(this Component ctr)
        {
            if (ctr == null)
                return;
            if (ctr is ToolStripItem)
            {
                var tsi = ctr as ToolStripItem;
                tsi.Text = tsi.Text.CultureString();
                if (tsi is ToolStripDropDownItem)
                {
                    var tsid = tsi as ToolStripDropDownItem;
                    if (tsid.DropDownItems != null && tsid.DropDownItems.Count > 0)
                    {
                        foreach (ToolStripItem d in tsid.DropDownItems)
                        {
                            d.UseCulture();
                        }
                    }
                }
            }
            else if( ctr is NotifyIcon)
            {
                var nfi = ctr as NotifyIcon;
                nfi.Text = nfi.Text.CultureString();
                nfi.ContextMenuStrip.UseCulture();
            }
        }
    }
}
